package com.wangsd.web.coupon.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.wangsd.common.basicbean.Result;
import com.wangsd.common.enums.ResultCode;
import com.wangsd.web.MyController;
import com.wangsd.web.coupon.entity.Category;
import com.wangsd.web.coupon.service.CategoryService;
import com.wangsd.web.sys.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.wangsd.common.enums.ResultCode.PRARM_NOT_NULL;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author generator
 * @since 2019-09-20
 */
@Api(value = "category", tags = "类别管理")
@RestController
@RequestMapping("/coupon/category")
@Slf4j
public class CategoryController extends MyController {

    @Autowired
    CategoryService categoryService;

    @ApiOperation(value = "查询类别列表", notes = "查询类别列表", httpMethod = "GET")
    @PostMapping("/selectList/{pageNum}/{pageSize}")
    public Result selectList(@ApiParam(value = "当前页", required = true) @PathVariable Integer pageNum,
                             @ApiParam(value = "每页大小", required = true) @PathVariable Integer pageSize,
                             @RequestBody @ApiParam(name = "category", value = "传入json格式") Category category) {
        SysUser userInfo = getUserInfo();
        IPage<Category> categoryIPage = categoryService.selectPage(userInfo.getCode(), pageNum, pageSize, category);
        return Result.success(categoryIPage);

    }

    @ApiOperation(value = "删除类别", notes = "删除类别", httpMethod = "GET", response = Result.class)
    @GetMapping("/delete/{id}")
    public Result delete(@ApiParam(value = "类别id", required = true) @PathVariable Long id) {
        boolean b = categoryService.removeById(id);
        return Result.success(b);
    }

    @ApiOperation(value = "新增或修改类别信息", notes = "新增或修改类别信息", httpMethod = "POST", response = Result.class)
    @PostMapping("/addOrUpdate")
    public Result addOrUpdate(@RequestBody Category category) {

        if (category.getCompanyId() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "商圈" + PRARM_NOT_NULL.getMessage());
        }

        if (category.getName() == null) {
            return new Result(PRARM_NOT_NULL.getCode(), "类别名称" + PRARM_NOT_NULL.getMessage());
        }

        Category repeatCategory = categoryService.selectRepeat(category.getName(), category.getId(), category.getCompanyId());
        if (repeatCategory != null) {
            return Result.message(ResultCode.NAME_REPEAT);
        }
        SysUser userInfo = getUserInfo();
        if (99 != userInfo.getType()) {
            category.setCode(userInfo.getCode());
        }
        categoryService.addOrUpdate(category);
        return Result.success();
    }

    @ApiOperation(value = "下拉列表查询类别", notes = "下拉列表查询类别", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectAll")
    public Result selectAll() {
        List<Map<String, Object>> maps = categoryService.selectAllByUserCode(getUserInfo().getCode());
        return Result.success(maps);
    }

    @ApiOperation(value = "通过商圈id下拉列表查询类别", notes = "通过商圈id下拉列表查询类别", httpMethod = "GET", response = Result.class)
    @GetMapping("/selectCategory/{companyId}")
    public Result selectCategory( @PathVariable Long companyId) {
        List<Category> categoryList = categoryService.list(new QueryWrapper<Category>().eq("company_id", companyId));
        return Result.success(categoryList);
    }
}

